<?php

namespace App\Console\Commands;

use EasyWeChat\Factory;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\Config;
use Illuminate\Support\Facades\DB;

class Progress extends Command
{

    public $id;

    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    #protected $signature = 'Progress {id?} {progress?}';
    protected $signature = 'Progress {id} {progress}';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = '服务进程通知';

    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle()
    {

        $config = Config::get('wechat.chat');

        $app = Factory::officialAccount($config);

        $id = $this->argument('id');
        $progress = $this->argument('progress');

        $baby = DB::table('babies')->where('id', $id)->first();

        if ($baby) {
            $openDao = DB::table('opens')->where('user_id', $baby->user_id)->where('source','wx')->first();
            switch ($progress) {
                case 0:
                    #break;
                case 1:
                    $date = date('Y年m月d日');
                    $progress = Config::get('constant.progress.'.$progress);
                    $url = '';
                    $result = $app->template_message->send([
                        'touser' => $openDao->open_id,
                        'template_id' => 'LBKnPs0FSBtXYIhGBkc0yEIlmjkuQfXRSJzAB6nuqf0',
                        'url' => $url,
                        'miniprogram' => [
                            'appid' => Config::get('wechat.mini.app_id'),
                            'path' => '/partials/orderDetail/orderDetail?id='.$id,
                        ],
                        'data' => [
                            'first' => ['您好, 您的样本已收到', '#224b8f'],
                            'keyword1' => [$progress, '#224b8f'],
                            'keyword2' => [$date, '#224b8f'],
                            'remark' => ['value'=>"\r\n".'您可以在“我的--我的订单”中查询进度,感谢您使用安谱利产品,我们将用心为您服务', 'color'=>'red'],
                        ],
                    ]);
                    /**
                    array:3 [▼
                        "errcode" => 0
                        "errmsg" => "ok"
                        "msgid" => 771105059299016704
                    ]
                    **/
                    break;
                case 2:
                    break;
                case 3:
                    break;
                default:
                    break;
            }
        }

        print 'Send Message Ok!';

    }

}
